package com.alex.spring.springBase;

import com.alex.spring.annotation.Student;
import org.junit.Test;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.context.support.XmlWebApplicationContext;

/**
 * Describe:
 * Created by pengp on 2018/10/4.
 */
public class Spring01 {

    @Test
    public void test01(){
        XmlBeanFactory factory = new XmlBeanFactory
                (new ClassPathResource("Beans.xml"));
        HelloWorld obj = (HelloWorld) factory.getBean("helloWorld");
        obj.getMessage();
    }

    @Test
    public void test02(){
        ApplicationContext context =
                new ClassPathXmlApplicationContext("Beans.xml");
        HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
        obj.getMessage();
    }

    @Test
    public void test03(){
        ApplicationContext context = new FileSystemXmlApplicationContext
                ("E:\\software\\ideaWorkSpace\\java-simple\\src\\main\\resources\\Beans.xml");
        HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
        obj.getMessage();
    }

    @Test
    public void test04(){
        //----/WEB-INF/applicationContext.xml
        ApplicationContext context = new XmlWebApplicationContext();
        HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
        obj.getMessage();
    }

    @Test
    public void test05(){
        ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
        HelloWorld objA = (HelloWorld) context.getBean("helloWorld");
        objA.setMessage("I'm object A");
        objA.getMessage();
        HelloWorld objB = (HelloWorld) context.getBean("helloWorld");
        objB.getMessage();
        HelloWorld objA2 = (HelloWorld) context.getBean("helloWorld2");
        objA2.setMessage("I'm object A2");
        objA2.getMessage();
        HelloWorld objB2 = (HelloWorld) context.getBean("helloWorld2");
        objB2.getMessage();
    }

    @Test
    public void test06(){
        AbstractApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
        HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
        obj.getMessage();
        context.registerShutdownHook();
    }
    @Test
    public void test07(){
        AbstractApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
        HelloWorld obj = (HelloWorld) context.getBean("helloWorld2");
        obj.getMessage();
        /**
         * ShutdownHook(Thread hook)方法，可以注册一个JVM关闭的钩子，这个钩子可以在以下几种场景被调用：
         * 1）程序正常退出
         * 2）使用System.exit()
         * 3）终端使用Ctrl+C触发的中断
         * 4）系统关闭
         * 5）使用Kill pid命令干掉进程
         */
        context.registerShutdownHook();
    }
    @Test
    public void test08(){
        AbstractApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
        HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
        obj.getMessage();
        context.registerShutdownHook();
    }
    @Test
    public void test09(){
        ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");

        HelloWorld objA = (HelloWorld) context.getBean("helloWorld");

        objA.getMessage1();
        objA.getMessage2();

        HelloIndia objB = (HelloIndia) context.getBean("helloIndia");
        objB.getMessage1();
        objB.getMessage2();
        objB.getMessage3();
    }
    @Test
    public void test10(){
        ApplicationContext context =
                new ClassPathXmlApplicationContext("Beans.xml");
        TextEditor te = (TextEditor) context.getBean("textEditor");
        te.spellCheck();
    }
    @Test
    public void test11(){
        ApplicationContext context =
                new ClassPathXmlApplicationContext("Beans.xml");
        JavaCollection jc=(JavaCollection)context.getBean("javaCollection");
        jc.getAddressList();
        jc.getAddressSet();
        jc.getAddressMap();
        jc.getAddressProp();
    }


}
